package com.bnc.comps;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.beans.PropertyVetoException;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import com.bnc.images.ImageHelper;
import com.bnc.menu.BNCPanel;
import com.bnc.menu.attendance.Attendance;
import com.bnc.menu.member.Member;
import com.bnc.menu.notice.Notice;
import com.bnc.menu.schedule.Schedule;
import com.bnc.menu.stats.Stats;
import com.bnc.props.AppProperties;
import com.bnc.props.MenuProperties;

public class InternalFramePanel extends JPanel 
{
    private JDesktopPane desktopPane = null;
    private Image backImage = null; //member variable
    
    private int windowCount = 0;
    private int selectedCount = 0;
    
    public InternalFramePanel() 
    {
        super( new BorderLayout() );
        init();
    }
    
    private void init() 
    {
        setName( "Frames" );
        initControls();
    }
    
    private void initControls() 
    {
        desktopPane = new JDesktopPane();
        add( "Center", desktopPane );
        
        setBackgroundImage( AppProperties.BACK_IMAGE_01 );
    }
    
    public void setBackgroundImage( String imageName )
    {
    	backImage = ImageHelper.loadImage( imageName ).getImage();
    	desktopPane.setOpaque( false );
        desktopPane.paintComponents( desktopPane.getGraphics() );
    }
    
    public void paintComponent( Graphics g )
    {
       if( backImage == null ) super.paintComponent(g);
       else
       {
    	   g.drawImage( backImage, 0, 0, null );
    	   updateUI();
//          Graphics2D g2d = ( Graphics2D )g;
//
//          //scale the image to fit the size of the Panel
//          double mw = backImage.getWidth( null );
//          double mh = backImage.getHeight( null );
//
//          double sw = getWidth() / mw;
//          double sh = getHeight() / mh;
//
//          g2d.scale( sw, sh );
//          g2d.drawImage( backImage, 0, 0, null );
//          
//          desktopPane.updateUI();
       }
    }
    
    public void setInternalFrame( String title ) 
    {
    	JInternalFrame jif = getExistInternalFrame( title );
    	if ( jif == null )
    	{
    		BNCPanel panel = getBNCPanel( title );
    		createInternalFrame( title, panel );
    	}
    	else
    	{
    		setSeleted( jif, true );
    	}
    }
    
    private JInternalFrame createInternalFrame( String title, BNCPanel panel ) 
    {
        JInternalFrame jif = new JInternalFrame();
        
        windowCount = desktopPane.getComponentCount();
        int pos = AppProperties.INTERNAL_NEXT_GAP * ( windowCount % 10 );
        Dimension size = getBNCPanelSize( title );
        
        jif.setTitle( title );
        jif.setClosable( true );
        jif.setMaximizable( false );
        jif.setIconifiable( true );
        jif.setResizable( false );
        jif.setBounds( pos, pos, size.width, size.height );
        
        desktopPane.add( jif, 0 );
        setSeleted( jif, true );
        
        Container con = jif.getContentPane();
        con.add( panel );
        
        return jif;
    }
    
    private JInternalFrame getExistInternalFrame( String title )
    {
    	JInternalFrame jif = null;
    	JInternalFrame jfList[] = desktopPane.getAllFrames();
    	for ( JInternalFrame jf : jfList )
    	{
    		if ( jf.getTitle() == title )
    		{
    			jif = jf;
    			break;
    		}
    	}
    	
    	return jif;
    }
    
    private void setSeleted( JInternalFrame jif, boolean isSelected )
    {
    	try 
        {
            jif.setSelected( isSelected );
        }
        catch ( PropertyVetoException e ) {}
        
        jif.show();
    }
    
    private Dimension getBNCPanelSize( String title )
    {
    	Dimension dim = new Dimension();
    	
    	if ( title == MenuProperties.MENU_ATTENDANCE )
    	{
    		dim.width = AppProperties.INTERNAL_ATTENDANCE_WIDTH;
        	dim.height = AppProperties.INTERNAL_ATTENDANCE_HEIGHT;
    	}
    	else if ( title == MenuProperties.MENU_SCHEDULE )
    	{
    		dim.width = AppProperties.INTERNAL_SCHEDULE_WIDTH;
        	dim.height = AppProperties.INTERNAL_SCHEDULE_HEIGHT;
    	}
    	else if ( title == MenuProperties.MENU_NOTICE )
    	{
    		dim.width = AppProperties.INTERNAL_NOTICE_WIDTH;
        	dim.height = AppProperties.INTERNAL_NOTICE_HEIGHT;
    	}
    	else if ( title == MenuProperties.MENU_MEMBER )
    	{
    		dim.width = AppProperties.INTERNAL_MEMBER_WIDTH;
        	dim.height = AppProperties.INTERNAL_MEMBER_HEIGHT;
    	}
    	else if ( title == MenuProperties.MENU_STATS )
    	{
    		dim.width = AppProperties.INTERNAL_STATS_WIDTH;
        	dim.height = AppProperties.INTERNAL_STATS_HEIGHT;
    	}
    	
    	return dim;
    }
    
    private BNCPanel getBNCPanel( String title )
    {
    	BNCPanel panel = null;
    	
    	System.out.println( "PANEL TITLE : " + title );
    	
//    	if ( title == MenuProperties.MENU_MAIN ) 
//    		panel = new Main();
    	if ( title == MenuProperties.MENU_ATTENDANCE )
    		panel = new Attendance();
    	else if ( title == MenuProperties.MENU_SCHEDULE )
    		panel = new Schedule();
    	else if ( title == MenuProperties.MENU_NOTICE )
    		panel = new Notice();
    	else if ( title == MenuProperties.MENU_MEMBER )
    		panel = new Member();
    	else if ( title == MenuProperties.MENU_STATS )
    		panel = new Stats();
    	
    	if ( panel != null ) panel.setTitle( title );
    	
    	return panel;
    }
}
